// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat;

import java.util.ArrayList;
import java.util.HashSet;
import zombie.characters.IsoPlayer;
import zombie.characters.Talker;
import zombie.iso.objects.IsoRadio;
import zombie.iso.objects.IsoTelevision;
import zombie.network.GameServer;
import zombie.radio.ZomboidRadio;
import zombie.ui.TextDrawObject;
import zombie.ui.UIFont;
import zombie.vehicles.VehiclePart;

public class ChatElement implements Talker {
    protected ChatElement.PlayerLines[] playerLines = new ChatElement.PlayerLines[4];
    protected ChatElementOwner owner;
    protected float historyVal = 1.0F;
    protected boolean historyInRange = false;
    protected float historyRange = 15.0F;
    protected boolean useEuclidean = true;
    protected boolean hasChatToDisplay = false;
    protected int maxChatLines = -1;
    protected int maxCharsPerLine = -1;
    protected String sayLine = null;
    protected String sayLineTag = null;
    protected TextDrawObject sayLineObject = null;
    protected boolean Speaking = false;
    protected String talkerType = "unknown";
    public static boolean doBackDrop = true;
    public static NineGridTexture backdropTexture;
    private int bufferX = 0;
    private int bufferY = 0;
    private static ChatElement.PlayerLinesList[] renderBatch = new ChatElement.PlayerLinesList[4];
    private static HashSet<String> noLogText = new HashSet();

    public ChatElement(ChatElementOwner chatowner, int numberoflines, String talkertype) {
        this.owner = chatowner;
        this.setMaxChatLines(numberoflines);
        this.setMaxCharsPerLine(75);
        this.talkerType = talkertype != null ? talkertype : this.talkerType;
        if (backdropTexture == null) {
            backdropTexture = new NineGridTexture("NineGridBlack", 5);
        }
    }

    public void setMaxChatLines(int num) {
        num = num < 1 ? 1 : (num > 10 ? 10 : num);
        if (num != this.maxChatLines) {
            this.maxChatLines = num;

            for (int _int = 0; _int < this.playerLines.length; _int++) {
                this.playerLines[_int] = new ChatElement.PlayerLines(this.maxChatLines);
            }
        }
    }

    public int getMaxChatLines() {
        return this.maxChatLines;
    }

    public void setMaxCharsPerLine(int maxChars) {
        for (int _int = 0; _int < this.playerLines.length; _int++) {
            this.playerLines[_int].setMaxCharsPerLine(maxChars);
        }

        this.maxCharsPerLine = maxChars;
    }

    @Override
    public boolean IsSpeaking() {
        return this.Speaking;
    }

    @Override
    public String getTalkerType() {
        return this.talkerType;
    }

    public void setTalkerType(String type) {
        this.talkerType = type == null ? "" : type;
    }

    @Override
    public String getSayLine() {
        return this.sayLine;
    }

    public String getSayLineTag() {
        return this.Speaking && this.sayLineTag != null ? this.sayLineTag : "";
    }

    public void setHistoryRange(float range) {
        this.historyRange = range;
    }

    public void setUseEuclidean(boolean b) {
        this.useEuclidean = b;
    }

    public boolean getHasChatToDisplay() {
        return this.hasChatToDisplay;
    }

    protected float getDistance(IsoPlayer isoPlayer) {
        if (isoPlayer == null) {
            return -1.0F;
        } else {
            return this.useEuclidean
                ? (float)Math.sqrt(Math.pow((double)(this.owner.getX() - isoPlayer.x), 2.0) + Math.pow((double)(this.owner.getY() - isoPlayer.y), 2.0))
                : Math.abs(this.owner.getX() - isoPlayer.x) + Math.abs(this.owner.getY() - isoPlayer.y);
        }
    }

    protected boolean playerWithinBounds(IsoPlayer isoPlayer, float _float) {
        return isoPlayer == null
            ? false
            : isoPlayer.getX() > this.owner.getX() - _float
                && isoPlayer.getX() < this.owner.getX() + _float
                && isoPlayer.getY() > this.owner.getY() - _float
                && isoPlayer.getY() < this.owner.getY() + _float;
    }

    public void SayDebug(int n, String text) {
        if (!GameServer.bServer && n >= 0 && n < this.maxChatLines) {
            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                IsoPlayer isoPlayer = IsoPlayer.players[_int];
                if (isoPlayer != null) {
                    ChatElement.PlayerLines playerLinesx = this.playerLines[_int];
                    if (n < playerLinesx.chatLines.length) {
                        if (playerLinesx.chatLines[n].getOriginal() != null && playerLinesx.chatLines[n].getOriginal().equals(text)) {
                            playerLinesx.chatLines[n].setInternalTickClock((float)playerLinesx.lineDisplayTime);
                        } else {
                            playerLinesx.chatLines[n].setSettings(true, true, true, true, true, true);
                            playerLinesx.chatLines[n].setInternalTickClock((float)playerLinesx.lineDisplayTime);
                            playerLinesx.chatLines[n].setCustomTag("default");
                            playerLinesx.chatLines[n].setDefaultColors(1.0F, 1.0F, 1.0F, 1.0F);
                            playerLinesx.chatLines[n].ReadString(UIFont.Medium, text, this.maxCharsPerLine);
                        }
                    }
                }
            }

            this.sayLine = text;
            this.sayLineTag = "default";
            this.hasChatToDisplay = true;
        }
    }

    @Override
    public void Say(String line) {
        this.addChatLine(line, 1.0F, 1.0F, 1.0F, UIFont.Dialogue, 25.0F, "default", false, false, false, false, false, true);
    }

    public void addChatLine(String msg, float r, float g, float b, float baseRange) {
        this.addChatLine(msg, r, g, b, UIFont.Dialogue, baseRange, "default", false, false, false, false, false, true);
    }

    public void addChatLine(String msg, float r, float g, float b) {
        this.addChatLine(msg, r, g, b, UIFont.Dialogue, 25.0F, "default", false, false, false, false, false, true);
    }

    public void addChatLine(
        String msg,
        float r,
        float g,
        float b,
        UIFont font,
        float baseRange,
        String customTag,
        boolean bbcode,
        boolean img,
        boolean icons,
        boolean colors,
        boolean fonts,
        boolean equalizeHeights
    ) {
        if (!GameServer.bServer) {
            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                IsoPlayer isoPlayer = IsoPlayer.players[_int];
                if (isoPlayer != null
                    && (
                        !isoPlayer.Traits.Deaf.isSet()
                            || (
                                this.owner instanceof IsoTelevision
                                    ? ((IsoTelevision)this.owner).isFacing(isoPlayer)
                                    : !(this.owner instanceof IsoRadio) && !(this.owner instanceof VehiclePart)
                            )
                    )) {
                    float _float = this.getScrambleValue(isoPlayer, baseRange);
                    if (_float < 1.0F) {
                        ChatElement.PlayerLines playerLinesx = this.playerLines[_int];
                        TextDrawObject textDrawObject = playerLinesx.getNewLineObject();
                        if (textDrawObject != null) {
                            textDrawObject.setSettings(bbcode, img, icons, colors, fonts, equalizeHeights);
                            textDrawObject.setInternalTickClock((float)playerLinesx.lineDisplayTime);
                            textDrawObject.setCustomTag(customTag);
                            String string;
                            if (_float > 0.0F) {
                                string = ZomboidRadio.getInstance().scrambleString(msg, (int)(100.0F * _float), true, "...");
                                textDrawObject.setDefaultColors(0.5F, 0.5F, 0.5F, 1.0F);
                            } else {
                                string = msg;
                                textDrawObject.setDefaultColors(r, g, b, 1.0F);
                            }

                            textDrawObject.ReadString(font, string, this.maxCharsPerLine);
                            this.sayLine = msg;
                            this.sayLineTag = customTag;
                            this.hasChatToDisplay = true;
                        }
                    }
                }
            }
        }
    }

    protected float getScrambleValue(IsoPlayer isoPlayer, float float2) {
        if (this.owner == isoPlayer) {
            return 0.0F;
        } else {
            float float0 = 1.0F;
            boolean boolean0 = false;
            boolean boolean1 = false;
            if (this.owner.getSquare() != null && isoPlayer.getSquare() != null) {
                if (isoPlayer.getBuilding() != null
                    && this.owner.getSquare().getBuilding() != null
                    && isoPlayer.getBuilding() == this.owner.getSquare().getBuilding()) {
                    if (isoPlayer.getSquare().getRoom() == this.owner.getSquare().getRoom()) {
                        float0 = (float)((double)float0 * 2.0);
                        boolean1 = true;
                    } else if (Math.abs(isoPlayer.getZ() - this.owner.getZ()) < 1.0F) {
                        float0 = (float)((double)float0 * 2.0);
                    }
                } else if (isoPlayer.getBuilding() != null || this.owner.getSquare().getBuilding() != null) {
                    float0 = (float)((double)float0 * 0.5);
                    boolean0 = true;
                }

                if (Math.abs(isoPlayer.getZ() - this.owner.getZ()) >= 1.0F) {
                    float0 = (float)((double)float0 - (double)float0 * (double)Math.abs(isoPlayer.getZ() - this.owner.getZ()) * 0.25);
                    boolean0 = true;
                }
            }

            float float1 = float2 * float0;
            float float3 = 1.0F;
            if (float0 > 0.0F && this.playerWithinBounds(isoPlayer, float1)) {
                float float4 = this.getDistance(isoPlayer);
                if (float4 >= 0.0F && float4 < float1) {
                    float float5 = float1 * 0.6F;
                    if (!boolean1 && (boolean0 || !(float4 < float5))) {
                        if (float1 - float5 != 0.0F) {
                            float3 = (float4 - float5) / (float1 - float5);
                            if (float3 < 0.2F) {
                                float3 = 0.2F;
                            }
                        }
                    } else {
                        float3 = 0.0F;
                    }
                }
            }

            return float3;
        }
    }

    protected void updateChatLines() {
        this.Speaking = false;
        boolean _boolean = false;
        if (this.hasChatToDisplay) {
            this.hasChatToDisplay = false;

            for (int int0 = 0; int0 < IsoPlayer.numPlayers; int0++) {
                float float0 = 1.25F * zombie.GameTime.getInstance().getMultiplier();
                int int1 = this.playerLines[int0].lineDisplayTime;

                for (TextDrawObject textDrawObject : this.playerLines[int0].chatLines) {
                    float float1 = textDrawObject.updateInternalTickClock(float0);
                    if (!(float1 <= 0.0F)) {
                        this.hasChatToDisplay = true;
                        if (!_boolean && !textDrawObject.getCustomTag().equals("radio")) {
                            float float2 = float1 / ((float)int1 / 2.0F);
                            if (float2 >= 1.0F) {
                                this.Speaking = true;
                            }

                            _boolean = true;
                        }

                        float0 *= 1.2F;
                    }
                }
            }
        }

        if (!this.Speaking) {
            this.sayLine = null;
            this.sayLineTag = null;
        }
    }

    protected void updateHistory() {
        if (this.hasChatToDisplay) {
            this.historyInRange = false;
            IsoPlayer isoPlayer = IsoPlayer.getInstance();
            if (isoPlayer != null) {
                if (isoPlayer == this.owner) {
                    this.historyVal = 1.0F;
                } else {
                    if (this.playerWithinBounds(isoPlayer, this.historyRange)) {
                        this.historyInRange = true;
                    } else {
                        this.historyInRange = false;
                    }

                    if (this.historyInRange && this.historyVal != 1.0F) {
                        this.historyVal += 0.04F;
                        if (this.historyVal > 1.0F) {
                            this.historyVal = 1.0F;
                        }
                    }

                    if (!this.historyInRange && this.historyVal != 0.0F) {
                        this.historyVal -= 0.04F;
                        if (this.historyVal < 0.0F) {
                            this.historyVal = 0.0F;
                        }
                    }
                }
            }
        } else if (this.historyVal != 0.0F) {
            this.historyVal = 0.0F;
        }
    }

    public void update() {
        if (!GameServer.bServer) {
            this.updateChatLines();
            this.updateHistory();
        }
    }

    public void renderBatched(int playerNum, int x, int y) {
        this.renderBatched(playerNum, x, y, false);
    }

    public void renderBatched(int playerNum, int x, int y, boolean ignoreRadioLines) {
        if (playerNum < this.playerLines.length && this.hasChatToDisplay && !GameServer.bServer) {
            this.playerLines[playerNum].renderX = x;
            this.playerLines[playerNum].renderY = y;
            this.playerLines[playerNum].ignoreRadioLines = ignoreRadioLines;
            if (renderBatch[playerNum] == null) {
                renderBatch[playerNum] = new ChatElement.PlayerLinesList();
            }

            renderBatch[playerNum].add(this.playerLines[playerNum]);
        }
    }

    public void clear(int playerIndex) {
        this.playerLines[playerIndex].clear();
    }

    public static void RenderBatch(int playerNum) {
        if (renderBatch[playerNum] != null && renderBatch[playerNum].size() > 0) {
            for (int _int = 0; _int < renderBatch[playerNum].size(); _int++) {
                ChatElement.PlayerLines playerLinesx = (ChatElement.PlayerLines)renderBatch[playerNum].get(_int);
                playerLinesx.render();
            }

            renderBatch[playerNum].clear();
        }
    }

    public static void NoRender(int playerNum) {
        if (renderBatch[playerNum] != null) {
            renderBatch[playerNum].clear();
        }
    }

    public static void addNoLogText(String text) {
        if (text != null && !text.isEmpty()) {
            noLogText.add(text);
        }
    }

    class PlayerLines {
        protected int lineDisplayTime = 314;
        protected int renderX = 0;
        protected int renderY = 0;
        protected boolean ignoreRadioLines = false;
        protected TextDrawObject[] chatLines;

        public PlayerLines(int int0) {
            this.chatLines = new TextDrawObject[int0];

            for (int int1 = 0; int1 < this.chatLines.length; int1++) {
                this.chatLines[int1] = new TextDrawObject(0, 0, 0, true, true, true, true, true, true);
                this.chatLines[int1].setDefaultFont(UIFont.Medium);
            }
        }

        public void setMaxCharsPerLine(int int1) {
            for (int int0 = 0; int0 < this.chatLines.length; int0++) {
                this.chatLines[int0].setMaxCharsPerLine(int1);
            }
        }

        public TextDrawObject getNewLineObject() {
            if (this.chatLines != null && this.chatLines.length > 0) {
                TextDrawObject textDrawObject = this.chatLines[this.chatLines.length - 1];
                textDrawObject.Clear();

                for (int _int = this.chatLines.length - 1; _int > 0; _int--) {
                    this.chatLines[_int] = this.chatLines[_int - 1];
                }

                this.chatLines[0] = textDrawObject;
                return this.chatLines[0];
            } else {
                return null;
            }
        }

        public void render() {
            if (!GameServer.bServer) {
                if (ChatElement.this.hasChatToDisplay) {
                    int _int = 0;

                    for (TextDrawObject textDrawObject : this.chatLines) {
                        if (textDrawObject.getEnabled()) {
                            if (textDrawObject.getWidth() > 0 && textDrawObject.getHeight() > 0) {
                                float float0 = textDrawObject.getInternalClock();
                                if (!(float0 <= 0.0F) && (!textDrawObject.getCustomTag().equals("radio") || !this.ignoreRadioLines)) {
                                    float float1 = float0 / ((float)this.lineDisplayTime / 4.0F);
                                    if (float1 > 1.0F) {
                                        float1 = 1.0F;
                                    }

                                    this.renderY = this.renderY - (textDrawObject.getHeight() + 1);
                                    boolean _boolean = textDrawObject.getDefaultFontEnum() != UIFont.Dialogue;
                                    if (ChatElement.doBackDrop && ChatElement.backdropTexture != null) {
                                        ChatElement.backdropTexture
                                            .renderInnerBased(
                                                this.renderX - textDrawObject.getWidth() / 2,
                                                this.renderY,
                                                textDrawObject.getWidth(),
                                                textDrawObject.getHeight(),
                                                0.0F,
                                                0.0F,
                                                0.0F,
                                                0.4F
                                            );
                                    }

                                    if (_int == 0) {
                                        textDrawObject.Draw((double)this.renderX, (double)this.renderY, _boolean, float1);
                                    } else if (ChatElement.this.historyVal > 0.0F) {
                                        textDrawObject.Draw((double)this.renderX, (double)this.renderY, _boolean, float1 * ChatElement.this.historyVal);
                                    }

                                    _int++;
                                }
                            } else {
                                _int++;
                            }
                        }
                    }
                }
            }
        }

        void clear() {
            if (ChatElement.this.hasChatToDisplay) {
                ChatElement.this.hasChatToDisplay = false;

                for (int _int = 0; _int < this.chatLines.length; _int++) {
                    if (!(this.chatLines[_int].getInternalClock() <= 0.0F)) {
                        this.chatLines[_int].Clear();
                        this.chatLines[_int].updateInternalTickClock(this.chatLines[_int].getInternalClock());
                    }
                }

                ChatElement.this.historyInRange = false;
                ChatElement.this.historyVal = 0.0F;
            }
        }
    }

    class PlayerLinesList extends ArrayList<ChatElement.PlayerLines> {
    }
}
